package com.mmlc.meimei.lichengkongjian.mine;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.Adapter.AllDiscountCouponAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.MyCouponRequestFilter;
import com.mmlc.network.responsebean.MyCouponResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/30.
 */
public class DiscountCouponFragment extends InterFragment implements AdapterView.OnItemClickListener {
    @Override
    protected int initResourceID() {
        return R.layout.fragment_discount_coupon;
    }

    /**
     * type  0:default;1:order form
     * index  0:unused;1:overdue;2:used
     */
    int type = 0, index = 0;

    @Override
    protected void initExtra() {
        super.initExtra();
        type = getArguments().getInt("type", 0);
        index = getArguments().getInt("index", 0);
    }

    PullToRefreshListView list_coupon;
    AllDiscountCouponAdapter adapter;

    @Override
    protected void initView() {
        list_coupon = ViewUtils.getView(view, R.id.list_coupon);

        adapter = new AllDiscountCouponAdapter(context, datas, index);
        list_coupon.setAdapter(adapter);
    }

    List<MyCouponResponseBean.CouponListBean> datas = new ArrayList();
    int pageNow = 0;

    boolean isOk = true;
    Handler refreshHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 0) {
                if (!isOk) {
                    list_coupon.onRefreshComplete();
                    isOk = true;
                }
            }
        }
    };

    @Override
    protected void initEvent() {
        super.initEvent();
        list_coupon.setMode(PullToRefreshBase.Mode.BOTH);
        list_coupon.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) throws InterruptedException {
                if (isOk) {
                    pageNow = 0;
                    initInfo();
                    refreshHandler.sendEmptyMessageDelayed(0, 3000);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (isOk) {
                    pageNow++;
                    initInfo();
                    refreshHandler.sendEmptyMessageDelayed(0, 3000);
                }
            }
        });

        list_coupon.setOnItemClickListener(this);
    }

    @Override
    protected void initInfo() {
        super.initInfo();
        isOk = false;

        final MyCouponRequestFilter filter = new MyCouponRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.type = String.valueOf(index + 1);
        filter.requestBean.pageNow = String.valueOf(pageNow);
        filter.offerErrorParams((ViewGroup) view);
        filter.isTransparence = true;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<MyCouponResponseBean>() {
            @Override
            public void onSuccess(MyCouponResponseBean result) {
                list_coupon.onRefreshComplete();
                isOk = true;

                if (pageNow == 0) datas.clear();
                datas.addAll(result.getData().getList());
                if (datas != null && datas.size() > 0)
                    adapter.notifyDataSetChanged();
                else filter.AddNoDataUI();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                list_coupon.onRefreshComplete();
                isOk = true;

                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (type == 0 || index != 0)
            return;
/*        Intent intent = new Intent();
        intent.putExtra("id", datas.get(position).getId());
        intent.putExtra("price", datas.get(position).getMoney());
        getActivity().setResult(200, intent);
        getActivity().finish();
        暂时没得用
        */
    }
}
